﻿define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 引入通用的城市学校过滤功能
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/index.js';
            document.head.appendChild(script);

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'slider/slider/index',
                    add_url: 'slider/slider/add',
                    edit_url: 'slider/slider/edit',
                    del_url: 'slider/slider/del',
                    multi_url: 'slider/slider/multi',
                    table: 'fs_slider',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: "city_id", title: "城市" },
                        { field: "school_id", title: "学校" },
                        { field: 'number', title: __('Number'), operate: 'LIKE' },
                        { field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'address', title: __('Address'), operate: 'LIKE' },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js?';
            document.head.appendChild(script);
            Controller.api.bindevent();
            Controller.api.initImageGroups('add');
            Controller.api.initOcationSelect();
            Controller.api.handleUrlParams();
        },
        edit: function () {
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js?';
            document.head.appendChild(script);
            Controller.api.bindevent();
            Controller.api.initImageGroups('edit');
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                // 监听位置描述选择框变化，自动填入图片宽高
                $(document).on('change', '#c-number', function () {
                    var $selectedOption = $(this).find('option:selected');
                    var imageWidth = $selectedOption.attr('data-image-width');
                    var imageHeight = $selectedOption.attr('data-image-height');

                    // 如果选中的option有宽高属性，则填入对应字段
                    if (imageWidth) {
                        $('#c-image-width').val(imageWidth);
                    }
                    if (imageHeight) {
                        $('#c-image-height').val(imageHeight);
                    }
                });
            },
            
            // 初始化教学点下拉框
            initOcationSelect: function() {
                // 加载教学点列表
                $.ajax({
                    url: 'ocation/ocation/selectpage',
                    type: 'GET',
                    dataType: 'json',
                    success: function(response) {
                        if (response.code === 1 && response.data && response.data.list) {
                            var $select = $('#c-ocation_id');
                            $select.empty().append('<option value="">请选择教学点</option>');
                            
                            $.each(response.data.list, function(index, item) {
                                $select.append('<option value="' + item.id + '">' + item.name + '</option>');
                            });
                            
                            // 如果有URL参数中的ocation_id，自动选中
                            var urlParams = Controller.api.getUrlParams();
                            if (urlParams.ocation_id) {
                                $select.val(urlParams.ocation_id);
                            }
                        }
                    },
                    error: function() {
                    }
                });
            },
            
            // 处理URL参数
            handleUrlParams: function() {
                var urlParams = Controller.api.getUrlParams();
                
                // 设置位置描述
                if (urlParams.position_desc) {
                    $('#c-number').val(decodeURIComponent(urlParams.position_desc));
                }
                
                // 设置默认图片尺寸（教学点轮播图的标准尺寸）
                $('#c-image-width').val('1195');
                $('#c-image-height').val('607');
            },
            
            // 获取URL参数
            getUrlParams: function() {
                var params = {};
                var search = window.location.search;
                if (search) {
                    search = search.substring(1);
                    var pairs = search.split('&');
                    for (var i = 0; i < pairs.length; i++) {
                        var pair = pairs[i].split('=');
                        if (pair.length === 2) {
                            params[pair[0]] = pair[1];
                        }
                    }
                }
                return params;
            },

            // 初始化图片组管理功能
            initImageGroups: function (mode) {
                var imageCount = mode === 'add' ? 1 : 0;
                var maxImages = 5;

                // 更新图片计数显示
                function updateImageCount() {
                    $('#image-count').text(imageCount);
                    if (imageCount >= maxImages) {
                        $('#add-image-group').prop('disabled', true).addClass('disabled');
                    } else {
                        $('#add-image-group').prop('disabled', false).removeClass('disabled');
                    }
                }

                // 添加图片按钮事件
                $('#add-image-group').click(function () {
                    if (imageCount < maxImages) {
                        imageCount++;
                        updateImageCount();
                        // 这里可以添加更多图片组管理逻辑
                    }
                });

                // 初始化计数
                updateImageCount();
            }
        }
    };
    
    return Controller;
});
